package com.cinema.util;

import javax.servlet.http.HttpServletRequest;

//处理HttpServletRequest参数
public class HttpUtil {
    //将key对应的value值转换为整数
    public static int getInt(HttpServletRequest httpServletRequest, String key){
        try {
            return Integer.decode(httpServletRequest.getParameter(key));
        }catch (Exception e){
            return -1;
        }
    }
    //将key对应的value值转换为长整数
    public static Long getLong(HttpServletRequest httpServletRequest, String key){
        try {
            return Long.valueOf(httpServletRequest.getParameter(key));
        }catch (Exception e){
            return -1L;
        }
    }
    //将key对应的value值转换为double
    public static Double getDouble(HttpServletRequest httpServletRequest,String key){
        try {
            return Double.valueOf(httpServletRequest.getParameter(key));
        }catch (Exception e){
            return -1d;
        }
    }
    //将key对应的value值转换为bool型
    public static Boolean getBoolean(HttpServletRequest httpServletRequest, String key){
        try {
            return Boolean.valueOf(httpServletRequest.getParameter(key));
        }catch (Exception e){
            return false;
        }
    }
    public static String getString(HttpServletRequest httpServletRequest, String key){
        try {
            String result = httpServletRequest.getParameter(key);
            if(result != null){
                result = result.trim();
            }
            if ("".equals(result)){
                result = null;
            }
            return result;
        }catch (Exception e){
            return null;
        }
    }
}
